// pages/visitShop/visitShop.js
// pages/addVisiter/addVisiter.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopName:'',
    storevisitlist:[],
    CustomBar:app.globalData.CustomBar,
    jinDu:0,
    weiDu:0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyLocation()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.search()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inpShopName:function(e){
    this.setData({
      shopName:e.detail.value
    })
  },
  search:function(){
    let that = this;
    if(this.data.jinDu == 0 || this.data.weiDu ==0){
      this.getMyLocation();
      return;
    }
    let data = {'storename':this.data.shopName}
    app.api.getShopList(data).then(res => {
      if(res.code == 200){
        for(let item of res.rows){
          try{
            let itu = JSON.parse(item.itude)
            let dis = this.getDisance(itu.latitude,itu.longitude,this.data.jinDu,this.data.weiDu)
            item.dis = dis
          }catch(e){
            console.log(e)
          }
        }

        res.rows = res.rows.sort(function (a, b){
            return a.dis - b.dis
        });

        that.setData({
          storevisitlist : res.rows
        })
      }
    })
  },
  getMyLocation(){
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success:(res) =>{
        that.setData({
          jinDu : res.latitude, //经度
          weiDu : res.longitude //纬度
        })
        that.search()
      },
      fail : res => {
        console.log(res)
        if('getLocation:fail auth deny' == res.errMsg ){
          wx.showModal({
            title: '提示',
            content: '为获取用户定位授权',
            showCancel:false,
            success(res) {
              wx.navigateBack()
            }
          })
        }else if('getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' == res.errMsg){
          wx.showModal({
            title: '提示',
            content: '请打开手机定位',
            showCancel:false,
            success(res) {
              wx.navigateBack()
            }
          })
        }else{
          wx.showToast({
            title: res.errMsg,
          })
        }
      }
    })
  },
  getDisance(la1, lo1, la2, lo2) {
    let La1 = la1 * Math.PI / 180.0;
    let La2 = la2 * Math.PI / 180.0;
    let La3 = La1 - La2;
    let Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    s = s.toFixed(2);
    return s;
  },
})